/**
 * Created by Administrator on 2015/12/23.
 */

//问题模块
var get_question_module = function(){
    $.ajax({
        url: WebSite.AllQuestionPage.allQuestion_template,
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                var myTemplate = Handlebars.compile($("#allQuestion_questiontemplate").html());
                $("#questiontemplate").html(myTemplate(data));

                $(".tag-module-all").toggleClass("tag-selected");

                $(".tag-module-all").click(function(){
                    $(this).toggleClass("tag-selected");
                    $(".tag-module").removeClass("tag-selected");
                    getIndexContent();
                });

                $(".tag-module").click(function(){
                    $(".tag-module-all").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");

                    searchContext_all();
                });

            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};

//问题类型
var get_type_module = function(){
    $.ajax({
        url:WebSite.AllQuestionPage.allquestion_type,
        type:"post",
        dataType:"json",
        success:function(result){
            var data = result.resultData;
            console.log(data);
            var myTemplate_type = Handlebars.compile($("#allQuestion_typetemplate").html());
            $("#typetemplate").html(myTemplate_type(data));

            $(".tag-type-all").toggleClass("tag-selected");

            $(".tag-type-all").click(function(){
                $(this).toggleClass("tag-selected");
                $(".tag-type").removeClass("tag-selected");
                getIndexContent();
            });

            $(".tag-type").click(function(){
                $(".tag-type-all").removeClass("tag-selected");
                $(this).toggleClass("tag-selected");

                searchContext_all();
            });
        },
        error:function(){
            alert("程序出错，请联系管理员");
        }
    });
}

//版本模块
var get_version_module = function(){
    $.ajax({
        url: WebSite.AllQuestionPage.allQuestion_versiontemplate,
        type: "post",
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                var myTemplate_version = Handlebars.compile($("#allQuestion_versiontemplate").html());
                $("#versiontemplate").html(myTemplate_version(data));

                $(".tag-version-all").toggleClass("tag-selected");

                $(".tag-version-all").click(function(){
                    $(this).toggleClass("tag-selected");
                    $(".tag-version").removeClass("tag-selected");
                    getIndexContent();
                });

                $(".tag-version").click(function(){
                    $(".tag-version-all").removeClass("tag-selected");
                    $(this).toggleClass("tag-selected");
                    searchContext_all();
                });

            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};

/**
 * 点击模块的时候，搜索
 * @param pageno
 */
var searchContext_all = function(pageno){
    if(pageno==null){
        pageno=1;
    }
    var modules_arr  = [];
    var type_arr =[];
    var versions_arr = [];

    $(".tag-module.tag-selected").each(function(index, ele) {
        modules_arr.push($(ele).attr("data-module"));
    });
    $(".tag-type.tag-selected").each(function(index, ele) {
        type_arr.push($(ele).attr("data-type"));
    });
    $(".tag-version.tag-selected").each(function(index, ele) {
        versions_arr.push($(ele).attr("data-version"));

    });
    if(modules_arr.length == 0){
        modules_arr.push("");
    }
    if(type_arr.length ==0){
        type_arr.push("");
    }
    if(versions_arr.length == 0){
        versions_arr.push("");
    }
    var extParams = $.param({
        "vision": versions_arr,
        "model" : modules_arr,
        "type":type_arr
    }, true);
    WebSite.debug(WebSite.AllQuestionPage.search + "&" + extParams);
    $.ajax({
        url: WebSite.AllQuestionPage.search + "&" + extParams,
        type: "post",
        data: {"pageno":pageno,"methodname":'searchContext_all'},
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                for (var i = 0, len = data.length; i < len; i++) {
                    var record = data[i];
                    record.ALLQ_link = builALLQALink(record);
                }
                var pagemess=result.pagemess;
                $('#pagemes').html(pagemess);
                var myTemplate = Handlebars.compile($("#allQuestion_list").html());
                $("#question-template-list").html(myTemplate(data));
            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};

/**
 * 初始化的时候
 * @param pageno
 */
var getIndexContent = function(pageno){
    if(pageno==null){
        pageno=1;
    }

    if ($(".tag-module.tag-selected").size() < 1) {
        $(".tag-module-all").toggleClass("tag-selected");

    }

    if ($(".tag-type.tag-selected").size() < 1) {
        $(".tag-type-all").toggleClass("tag-selected");
    }

    if ($(".tag-version.tag-selected").size() < 1) {
        $(".tag-version-all").toggleClass("tag-selected");
    }

    var versions_arr = [];
    var modules_arr  = [];
    var type_arr = [];

    $(".tag-module.tag-selected").each(function(index, ele) {
        modules_arr.push($(ele).attr("data-module"));
    });
    $(".tag-type.tag-selected").each(function(index, ele) {
        type_arr.push($(ele).attr("data-type"));
    });
    $(".tag-version.tag-selected").each(function(index, ele) {
        versions_arr.push($(ele).attr("data-version"));
    });

    if(modules_arr.length == 0){
        modules_arr.push("");
    }
    if(type_arr.length == 0){
        type_arr.push("");
    }
    if(versions_arr.length ==0){
        versions_arr.push("");
    }
    var extParams = $.param({
        "model": modules_arr,
        "type": type_arr,
        "vision": versions_arr
    }, true);
    WebSite.debug(WebSite.AllQuestionPage.search + "&" + extParams);
    $.ajax({
        url: WebSite.AllQuestionPage.search + "&" + extParams,
        type: "post",
        data: {"pageno":pageno,"methodname":'getIndexContent'},
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                for (var i = 0, len = data.length; i < len; i++) {
                    var record = data[i];
                    record.ALLQ_link = builALLQALink(record);
                }
                var pagemess=result.pagemess;
                $('#pagemes').html(pagemess);
                var myTemplate = Handlebars.compile($("#allQuestion_list").html());
                $("#question-template-list").html(myTemplate(data));
            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
}

var builALLQALink = function(record) {

    var status = record.status;
    var pk_sool_question = record.pk_sool_question;

    return WebSite.ReplyerUnansweredPage.url+"&pk_question="+pk_sool_question;


    //if (status == WebSite.askerWaitToAnswerStatus) { //提问者待解答
    //    return WebSite.AskerUnansweredPage.url+"&pk_question="+pk_sool_question;
    //} else { //提问者待处理
    //    return WebSite.AskerHandlingPage.url+"&pk_question="+pk_sool_question;
    //}

}



//问题列表模块
var get_question_list = function (pageno) {
    if(pageno==null){
        pageno=1;
    }
    $.ajax({
        url: WebSite.AllQuestionPage.allquestion_list,
        type: "post",
        data: {"model":"","vision":"","type":"","pageno":pageno,"methodname":'get_question_list'},
        dataType: "json",
        success: function (result) {
            if (result.success) {
                var data = result.resultData;
                for (var i = 0, len = data.length; i < len; i++) {
                    var record = data[i];
                    record.ALLQ_link = builALLQALink(record);
                }
                var pagemess=result.pagemess;
                $('#pagemes').html(pagemess);
                var myTemplate = Handlebars.compile($("#allQuestion_list").html());
                $("#question-template-list").html(myTemplate(data));
            }
        },
        error: function () {
            alert("没有获得数据");
        }
    });
};



var registerHelper = function () {
    Handlebars.registerHelper("compare",function(v1,options){
        if(v1 == 1){
            return options.fn(this);
        }
        else{
            return options.inverse(this);
        }
    });
}

